$(function(){
    //初始化页面列表
    $("#claim_datagrid").datagrid({
        fit:true,
        url:"/claimBill/accidentAndClaimBill",
        fitColumns:true,
        striped:true,
        pagination:true,
        singleSelect:true,
        checkOnSelect:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',title:'编号', hidden:true},
            {field:'checkbox',checkbox:true,tilte:'选中',width:100},
            {field:'state',title:'当前状态',align:'center',width:100,formatter:function(value){
                if(value==1){
                    return "<p style='color: orange'>待验证</p>"
                }else if(value==2){
                    return "<p style='color: hotpink'>验证完,待填理赔单</p>"
                }else if(value==3){
                    return "<p style='color: mediumpurple'>理赔单完,待提审</p>"
                }else if(value==4){
                    return "<p style='color: #00bbee'>待审核</p>"
                }else if(value==5){
                    return "<p style='color: green'>审核完成</p>"
                }else if(value==6){
                    return "<p style='color: greenyellow'>支付完成</p>"
                }else if(value==7){
                    return "<p style='color: red'>支付失败</p>"
                }else if(value==8){
                    return "<p style='color: red'>审核驳回</p>"
                }else{
                    return "<p style='color: darkblue'>未知状态</p>"
                }
            }},
            {field:'reporterName',title:'报案人姓名',align:'center',width:100,formatter:function(value,rows,index){
                if(rows.client!=null){
                    return rows.client.clientname;
                }else{
                    return "";
                }
            }},
            {field:'tel',title:'联系方式',align:'center',width:100,formatter:function(value,rows,index){
                if(rows.client!=null){
                    return rows.client.tel;
                }else{
                    return "";
                }
            }},
            {field:'reportTime',title:'报案时间',align:'center',width:100},
            {field:'responsibility',title:'事故责任',align:'center',width:100},
            {field:'validateStaff',title:'验证员工',align:'center',width:100},

            {field:'insuranceNo',title:'保险单号',align:'center',width:100},
          /*  {field:'beneficiary',title:'受益人',align:'center',width:100,formatter:function (value,rows,index) {
                if(rows){
                    return rows.claimBill.beneficiary;
                }
            }},
            {field:'account',title:'账户',align:'center',width:100,formatter:function (value,rows,index) {
                if(rows){
                    return rows.claimBill.account;
                }
            }},
            {field:'amountpay',title:'赔偿金额',align:'center',width:100,formatter:function (value,rows,index) {
                if(rows){
                    return rows.claimBill.amountpay;
                }
            }},*/
            {field:'note',title:'备注',align:'center',width:100}
        ]]
    })
    //初始化页面列表--end

    //初始化案件详情弹窗
    $("#claim_dialog").dialog({
        width:400,
        height:400,
        closed:true
    })
    //初始化案件详情弹窗--end

    //初始化理赔明细弹窗
    $("#claimBill_dialog").dialog({
        width:400,
        height:400,
        buttons:"#bb",
        closed:true
    })
    //初始化案件详情弹窗--end

    //初始化案件详情下拉表格
    $("#client_grid").combogrid({
        panelWidth:450,
        url:'/client/queryFormalClient',
        idField:'id',
        textField:'clientname',
        fit:true,
        fitColumns:true,
        striped:true,
        singleSelect:true,
        checkOnSelect:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#accident_tb",
        columns:[[
            {field:'id',title:'客户编号',width:60,hidden:true},
            {field:'clientname',title:'客户姓名',width:100,align:'center'},
            {field:'gender',title:'性别',width:100,align:'center',formatter:function(value){
                if(value==1){
                    return "男";
                }else{
                    return "女";
                }
            }},
            {field:'tel',title:'练习方式',width:100,align:'center'},
            {field:'email',title:'邮箱',width:100,align:'center'},
            {field:'insuranceNo',title:'保险单号',width:100,align:'center'}
        ]]
    })
    //初始化案件详情下拉表格--end

})

//页面列表废弃事件
function remove(){
    var selectedRow = $("#claim_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要废弃的数据");
        return;
    }
    $.messager.confirm('确认','您确认想要废弃这条记录吗？',function(r){
        if (r){
            $.get("/claimBill/delete?id="+selectedRow.id,function (data) {
                if(data.success){
                    $("#claim_datagrid").datagrid("load");
                }else{
                    $.messager.alert("温馨提示",data.msg)
                }
            })
        }
    });
}
//页面列表废弃事件--end

//弹窗底部保存按钮
function save(){
    var id = $("#id").val();
    var url = "/claimBill/save"
    if(id){
        url="/claimBill/update"
    }
    $("#claimBill_form").form("submit",{
        url:url,
        success:function(data){
            data=$.parseJSON(data);
            if(data.success){
                $("#claim_datagrid").datagrid("load");
                $("#claimBill_dialog").dialog("close");
            }else{
                $.messager.alert("温馨提示",data.msg)
            }
        }
    })
}
//弹窗底部保存按钮--end

//列表页面查看案情事件
function details(){
    var selectedRow = $("#claim_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要查看的数据");
        return;
    }
    $("#claim_dialog").dialog("setTitle","<p style='text-align: center;color:deepskyblue;font-size: 13px;line-height:8px'>查看案件详情</p>");
    $("#claim_form").form("clear");
    $("#claim_dialog").dialog("open");
    selectedRow["client.id"]=selectedRow.client.id;
    $("#claim_form").form("load",selectedRow);
}
//列表页面查看案情事件--end

//弹窗底部关闭按钮
function closes(){
    $("#claim_dialog").dialog("close")
}
//弹窗底部关闭按钮--end

//刷新列表页面
function reload1(){
    $("#claim_datagrid").datagrid("reload");
}
//刷新列表页面--end

//提交审核
function submitUp() {
    var selectedRow = $("#claim_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要提审的数据");
        return;
    }
    var insuranceNo =selectedRow.insuranceNo;
    $.get("/claimBill/selectBillByInsuranceNo?insuranceNo="+insuranceNo,function (data) {
        if(data && data[0]){
            $.messager.confirm('确认','您确认想要提审这条记录吗？',function(r){
                if (r){
                    $.get("/claimBill/changeAuditState?id="+selectedRow.id,function (data) {
                        if(data.success){
                            $("#claim_datagrid").datagrid("load");
                        }else{
                            $.messager.alert("温馨提示",data.msg)
                        }
                    })
                }
            });
        }else{
            $.messager.alert("温馨提示","请先填写理赔明细表")
        }
    })

}
//提交审核--end

//页面填写理赔明细表
function writeBill() {
    var selectedRow = $("#claim_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要填写理赔明细的数据");
        return;
    }
    $("#claimBill_form").form("clear")
    $("#claimBill_dialog").dialog("setTitle","<p style='text-align: center;color:deepskyblue;font-size: 13px;line-height:8px'>填写理赔明细表</p>");
    $("#claimBill_dialog").dialog("open");
    selectedRow["client.id"]=selectedRow.client.id;
    selectedRow["accident_Id"]=selectedRow.id;
    selectedRow.id="";
    $("#claimBill_form").form("load",selectedRow);
}
//页面填写理赔明细表--end
// 页面编辑理赔明细表
function editBill() {
    var selectedRow = $("#claim_datagrid").datagrid("getSelected");
    if(!selectedRow){
        $.messager.alert("温馨提示","请选择要编辑理赔明细的数据");
        return;
    }
    if(selectedRow.state!=3){
        $.messager.alert("温馨提示","请先填写明细");
        return;
    }
    $("#claimBill_form").form("clear")
    $("#claimBill_dialog").dialog("setTitle","<p style='text-align: center;color:red;font-size: 13px;line-height:8px'>编辑理赔明细表</p>");
    $("#claimBill_dialog").dialog("open");
    $.get("/claimBill/selectBillByInsuranceNo?insuranceNo="+selectedRow.insuranceNo,function(data) {
        if(data){
            console.log(data)
            selectedRow.beneficiary=data[0].beneficiary;
            selectedRow.account=data[0].account;
            selectedRow.amountpay=data[0].amountpay;
            selectedRow.id=data[0].id;
            selectedRow["client.id"]=selectedRow.client.id;
            selectedRow["accident_Id"]=selectedRow.id;
            console.log(selectedRow);
            $("#claimBill_form").form("load",selectedRow);
        }
    })
}
//页面编辑理赔明细表--end